package ecom;

import java.io.Serializable;

//import javax.faces.bean.ManagedBean;

import java.util.ArrayList;
import java.util.List;
import javax.naming.NamingException;
//import org.objectweb.jonas.security.auth.callback.NoInputCallbackHandler;

import java.io.*;

//@ManagedBean
public class AdminStatistiques implements Serializable{
  public String listeIPS(){
    // systemCall("ssh user@machine.com");
    systemCall("sh call_top_IPs.sh");
    return "listeIPs";
  }

  // gicom, requete Hadoop
  public String systemCall(String command)  {

        System.out.println("Envoyer une requete ssh pour hadoop");

    StringBuffer result = new StringBuffer("");
    //    String command = "ls -lh > /tmp/log_command";
    //    ssh  -o "StrictHostKeyChecking no" -i id_rsa-pstam-keypair ubuntu@$url_instance 'source ~/.bashrc; chmod u+x test.sh ; sh ./test.sh'
    // Run the command
    try{
    
      Process p = Runtime.getRuntime().exec(command);  

      InputStream in = p.getInputStream();
      BufferedInputStream buf = new BufferedInputStream(in);
      InputStreamReader inread = new InputStreamReader(buf);
      BufferedReader bufferedreader = new BufferedReader(inread);
  
      // Read the output
      String line;
      while ((line = bufferedreader.readLine()) != null) {
        System.out.println(line);
      }
      // Check for failure
      try {
        if (p.waitFor() != 0) {
          System.err.println("exit value = " + p.exitValue());
        }
      } catch (InterruptedException e) {
        System.err.println(e);
      } finally {
        // Close the InputStream
        bufferedreader.close();
        inread.close();
        buf.close();
        in.close();
      }

    }catch (Exception e){
      System.out.println("Eh non !");
      e.printStackTrace();
    }

   
    
    return "hadoop";
  }
}//class